<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="消息标题" prop="msgTitle">
        <el-input
          v-model="queryParams.msgTitle"
          placeholder="消息标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="发送人工号" prop="accepterNo">
        <el-input
          v-model="queryParams.senderNo"
          placeholder="请输入发送人工号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="发送人" prop="msgAccepter">
        <el-input
          v-model="queryParams.msgSender"
          placeholder="请输入发送人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="消息状态" prop="msgStatus">
        <el-select v-model="queryParams.msgStatus" placeholder="消息状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="handleQuery"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="msgList" style="width: 100%;" :show-header="true">
      <el-table-column label="序号" align="center"  type="index"  :show-overflow-tooltip="false" />
      <el-table-column label="消息标题" align="center" prop="msgTitle" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-link :underline="false" type="primary" @click="handleUpdate(scope.row)">
            <span v-text="scope.row.msgTitle"></span>
          </el-link>
        </template>
      </el-table-column>
      <!--<el-table-column
        label="消息内容"
        align="center"
        prop="msgContent"
        :show-overflow-tooltip="true"
      />-->
      <el-table-column label="发送人" align="center" prop="msgSender"  />
      <el-table-column label="发送时间" align="center" prop="createTime">
        <!--        <template slot-scope="scope">-->
        <!--          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>-->
        <!--        </template>-->
      </el-table-column>
      <el-table-column label="接受人" align="center" prop="msgAccepter"  />
      <el-table-column label="处理时间" align="center" prop="processingTime" />
      <el-table-column
        label="消息状态"
        align="center"
        prop="msgStatus"
        :formatter="statusFormat"
      />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="initList"
    />

    <!-- 处理消息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="880px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="内容：">
              <!--              <el-input type="textarea" :rows="3" disabled  v-model="form.msgContent"></el-input>-->
              <div v-text="form.msgContent"></div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-show="isBtnVisible">处 理</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {indexListMsg, getMsgInfo, handlerMsg} from "@/api/common/msg";
  export default {
    name: "unreadMsg",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 消息表格数据
        msgList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 处理按钮初始显示
        isBtnVisible: true,
        // 状态数据字典
        statusOptions: [],
        // 日期范围
        dateRange: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          msgTitle: undefined,
          senderNo: undefined,
          msgSender: undefined,
          accepterNo: undefined,
          msgAccepter: undefined,
          msgStatus: undefined,
          searchValue: undefined
        },
        // 处理消息表单参数
        form: {},
      };
    },
    created() {
      //查询自己处理消息标识
      this.queryParams.searchValue = "index";
      //初始化消息列表
      this.initList();
      //查询消息字典状态
      this.getDicts("sys_msg_status").then(response => {
        this.statusOptions = response.data;
      });
    },
    methods: {
      //初始化未处理消息列表
      initList() {
        this.loading = true;
        indexListMsg(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.msgList = response.rows;
          this.total = response.total;
          this.loading = false;
          this.queryParams.searchValue = undefined;
        });
        // this.list = [{"title":"这是测试", "times":"2022-06-01 9:30"},{"title":"这是测试", "times":"2022-06-01 9:30"}];
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        //查询自己处理消息标识
        this.queryParams.searchValue = "index";
        this.initList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 查询详情修改操作 */
      handleUpdate(row) {
        const msgId = row.msgId || this.ids
        getMsgInfo(msgId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = this.form.msgTitle;
          if ("1" === this.form.msgStatus ) {
            this.isBtnVisible = false;
          }
        });
      },
      // 消息状态字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.msgStatus);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.isBtnVisible = true;
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.msgId != undefined) {
              //判断当前消息是否已处理
              if (this.form.msgStatus === "1") {
                this.msgError("重复处理！");
                return;
              }
              handlerMsg(this.form).then(response => {
                this.msgSuccess("已处理！");
                this.open = false;
                //查询未处理消息标识
                this.queryParams.searchValue = "index";
                //初始化消息列表
                this.initList();
              });
            }
          }
        });
      },
    }
  }
</script>
